<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['qiushi:followup:qiushiformitem:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="sort"/>
      <el-table-column label="问题标题" align="center" prop="label"/>

      <el-table-column label="问题类型" align="center" prop="type">
        <template slot-scope="scope">
          <div>{{scope.row.type === 'radio' ? '单选题' : scope.row.type === 'checkbox' ? '多选题' : scope.row.type === 'input' ? '问答题' : ''}}</div>
        </template>
      </el-table-column>

      <el-table-column label="是否必填" align="center" prop="required">
        <template slot-scope="scope">
          <p>{{required_dict[scope.row.required]['label']}}</p>
        </template>
      </el-table-column>

      <el-table-column label="提示文字" align="center" prop="placeholder"/>
      <el-table-column label="问题选项" align="center" prop="scheme"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['qiushi:followup:qiushiformitem:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['qiushi:followup:qiushiformitem:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加问答单项对话框 -->
    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问题标题" prop="label">
          <el-input v-model="form.label" placeholder="请输入问题标题"/>
        </el-form-item>

        <el-form-item label="问题类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择问题类型">
            <el-option
              v-for="item in type_dict"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否必填" prop="required">
          <el-select v-model="form.required" placeholder="请选择是否必填">
            <el-option
              v-for="item in required_dict"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <template v-if="form.type === '0' || form.type === '1'">
          <el-form-item label="问题选项" prop="scheme">
            <el-input v-model="form.scheme" type="textarea" placeholder="请用逗号“，”将选项隔开，例如：选项1，选项2，选项3"/>
          </el-form-item>
        </template>

        <template v-if="form.type === '2'">
          <el-form-item label="提示文字" prop="placeholder">
            <el-input v-model="form.placeholder" placeholder="请输入提示文字"/>
          </el-form-item>
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改问答单项对话框 -->
    <el-dialog :title="title" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问题序号" prop="label">
          <el-input v-model="form.sort" placeholder="请输入问题标题"/>
        </el-form-item>
        <el-form-item label="问题标题" prop="label">
          <el-input v-model="form.label" placeholder="请输入问题标题"/>
        </el-form-item>

        <el-form-item label="问题类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择问题类型">
            <el-option
              v-for="item in type_dict"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否必填" prop="required">
          <el-select v-model="form.required" placeholder="请选择是否必填">
            <el-option
              v-for="item in required_dict"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <template v-if="form.type === '0' || form.type === '1'">
          <el-form-item label="问题选项" prop="scheme">
            <el-input v-model="form.scheme" type="textarea" placeholder="请用逗号“，”将选项隔开，例如：选项1，选项2，选项3"/>
          </el-form-item>
        </template>

        <template v-if="form.type === '2'">
          <el-form-item label="提示文字" prop="placeholder">
            <el-input v-model="form.placeholder" placeholder="请输入提示文字"/>
          </el-form-item>
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listItem, getItem, delItem, addItem, updateItem} from "@/api/qiushi/followup/qiushiformitem";

export default {
  name: "Item",
  data() {
    return {
      // 字典
      type_dict: [{
        value: 'radio',
        label: '单选题'
      },{
        value: 'checkbox',
        label: '多选题'
      },{
        value: 'input',
        label: '问答题'
      }],
      required_dict: [{
        value: '0',
        label: '否'
      }, {
        value: '1',
        label: '是'
      }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问答单项表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open1: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        formId: null,
        type: null,
        label: null,
        required: null,
        placeholder: null,
        sort: null,
        scheme: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        formId: [
          {required: true, message: "表单id不能为空", trigger: "blur"}
        ],
        type: [
          {required: true, message: "表单项类型 不能为空", trigger: "change"}
        ],
        label: [
          {required: true, message: "表单项标题不能为空", trigger: "blur"}
        ],
        required: [
          {required: true, message: "是否必填 0否  1是不能为空", trigger: "blur"}
        ],
        delFlag: [
          {required: true, message: "0正常 1删除不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.queryParams.formId = window.location.search.split('=')[1]
    this.getList();
  },
  methods: {
    /** 查询问答单项列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then(response => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open1 = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        formId: window.location.search.split('=')[1],
        type: null,
        label: null,
        required: null,
        placeholder: null,
        sort: null,
        scheme: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open1 = true;
      this.title = "添加问答单项";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getItem(id).then(response => {
        this.form = response.data;
        this.open2 = true;
        this.title = "修改问答单项";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.type === '2') {
            this.form.scheme = '-'
          } else {
            this.form.placeholder = '-'
          }

          if (this.form.id != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open2 = false;
              this.getList();
            });
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除问答单项编号为"' + ids + '"的数据项？').then(function () {
        return delItem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('qiushi/followup/qiushiformitem/export', {
        ...this.queryParams
      }, `item_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
